﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace CompactMapper.Orm.UnitOfWork.Mappings
{
    internal abstract class Tree<T>
        where T : class, IEquatable<T>
    {
        public readonly T Node;
        private readonly List<Tree<T>> _children;

        protected Tree(T node)
            : this(node, new List<Tree<T>>())
        {
        }

        protected Tree(T node, IEnumerable<Tree<T>> children)
        {
            Node = node;
            _children = new List<Tree<T>>(children);
        }

        public IEnumerable<Tree<T>> Children
        {
            get { return _children; }
        }

        public Boolean Contains(T node)
        {
            return Node.Equals(node) || Children.Any(child => child.Contains(node));
        }

        public void Add(Tree<T> child)
        {
            _children.Add(child);
        }

        public void AddRange(IEnumerable<Tree<T>> children)
        {
            _children.AddRange(children);
        }
    }
}